package org.csu.canteen.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.csu.canteen.service.Impl.ItemServiceImpl;
import org.csu.canteen.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Data
@TableName("lineitem")
public class LineItem {
    @TableId(value = "orderid")
    private Integer orderId;

    @TableField(value = "linenum")
    private Integer lineNumber;

    private Integer quantity;

    @TableField(value = "itemid")
    private String itemId;

    @TableField(value = "unitprice")
    private BigDecimal unitPrice;


    @TableField(exist = false) // 非数据库字段
    private Item item;

    @TableField(exist = false) // 非数据库字段
    private BigDecimal total;

    public LineItem(int lineNumber, CartItem cartItem) {
        this.lineNumber = lineNumber;
        this.itemId = cartItem.getItemId();
        this.quantity = cartItem.getNumber();
        this.unitPrice = cartItem.getItem().getListPrice();
        this.item = cartItem.getItem();
    }

    public void setItem(Item item) {
        this.item = item;
        calculateTotal();
    }


    public void setQuantity(int quantity) {
        this.quantity = quantity;
        calculateTotal();
    }

    private void calculateTotal() {
        if (item != null && item.getListPrice() != null) {
            total = item.getListPrice().multiply(new BigDecimal(quantity));
        } else {
            total = null;
        }
    }



}
